--
-- Author: shunguo.chen
-- Date: 2016-06-17 11:42:18
--
-- DialogBase
--

local fileName = string.getModeName(...)
local DialogBase = class(fileName, function()
    local node = display.newModalDialog()
    node:setNodeEventEnabled(true)
    return node
end)

DialogBase.DIALOG_ROOT_RES = Res.ConfirmDialogUI

DialogBase._root = nil
DialogBase._uiBinding = nil
DialogBase._ui = nil

DialogBase._hint = nil
DialogBase._onConfirm = nil
DialogBase._onCancel = nil
DialogBase._data = nil

function DialogBase:ctor(hint, onConfirm, onCancel, data)
	self._hint = hint
	self._onConfirm = onConfirm
	self._onCancel = onCancel
	self._data = data

    self:initRoot()
    self:init()
    self:refresh()
    self:openAction()
end

function DialogBase:initRoot()
	--init root
	self._root, self._uiBinding = cc.CSLoader:createNode(self.DIALOG_ROOT_RES, true, true, true)
	self._root:setContentSize(display.width,display.height)
	ccui.Helper:doLayout(self._root)
	self._ui = display.uiDelegate(self._root)
	self:addChild(self._root)
end

function DialogBase:init()
	self._titleText = self._ui["tips/Panel_tlt/tlt"]
    self._conText = self._ui["tips/tips_txt"]
    self._okBtn = self._ui.Button_queding
	self._cancelBtn = self._ui.Button_quxiao

    self:setHint(self._hint)
    self:addClickEvent()
end

function DialogBase:addClickEvent()
	if not self._okBtn then return end
	self._okBtn:addClickEventListener(function(sender)
	    executeFunc(self._onConfirm)
		if not cc.isDead( self ) then
			self:removeFromParent()
		end
	end)

	if not self._cancelBtn then return end
    if self._onCancel then
    	self._cancelBtn:addClickEventListener(function(sender)
		    executeFunc(self._onCancel)
			if not cc.isDead( self ) then
				self:removeFromParent()
			end
		end)
	else
		self._cancelBtn:setVisible(false)
		self._okBtn:setPositionX(self._ui.bg:getPositionX())
    end
end

function DialogBase:openAction()
    display.playTimeline(self._root)
end

function DialogBase:refresh()

end

function DialogBase:setTitle(title)
	if title and self._titleText then
		self._titleText:setString(title)
	end
end

function DialogBase:setHint(hint)
	if hint and self._conText then
		self._conText:setString(hint)
	end
end

function DialogBase:setConfirmBtnVisible(b)
	if self._okBtn then
		self._okBtn:setVisible(b)
	end
end

function DialogBase:setConfirmText(text)
	if self._okBtn and text then
	    local label = self._okBtn:getChildByName("name")
	    if label then
	        label:setString(text)
	    end
	end
end

function DialogBase:setCancelText(text)
	if self._cancelBtn and text then
	    local label = self._cancelBtn:getChildByName("name")
	    if label then
	        label:setString(text)
	    end
	end
end

-- 超时按取消处理，移除界面
function DialogBase:setTimeout(time)
	if time > 0 then
		self:performWithDelay(function()
			executeFunc(self._onCancel)
			if not cc.isDead( self ) then
				self:removeFromParent()
			end
		end, time)
	end
end

function DialogBase:onEnter()

end

function DialogBase:onExit()
	self:remove()
	executeFunc(self._onClearView)
end

function DialogBase:onClearView(cb)
	self._onClearView = cb
end

function DialogBase:remove()

end

function DialogBase:replaceTextByRichText(richText)
	richText:setAnchorPoint(self._conText:getAnchorPoint())
	richText:ignoreContentAdaptWithSize(false)
	richText:setContentSize(self._conText:getContentSize().width,0)
	richText:formatText()
	local f_size = richText:getFormatSize()
	richText:setContentSize(f_size)
	richText:setAnchorPoint(self._conText:getAnchorPoint())
	richText:setPosition(self._conText:getPosition())
	richText:addTo(self._conText:getParent())
	self._conText:removeFromParent()
	self._conText = richText
end

return DialogBase
